package net.oschina.app.fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.hjpbean.CollectBean;
import net.oschina.app.hjpbean.UserBean;
import net.oschina.app.util.LogUtils;
import net.oschina.app.util.ZsfUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static net.oschina.app.Myapp.userBean;
import static net.oschina.app.R.string.favorite;

/**
 * Created by admin on 2017/7/17.
 */

public class CollectFragment extends MeFragmentView {
    @BindView(R.id.collect_rv)
    RecyclerView collectRv;
    Unbinder unbinder;
    private List<CollectBean.FavoriteBean> favorites;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
//        UserBean userBean = Myapp.userBean;
        UserBean userBean = (UserBean) ZsfUtils.getObject(getContext(), "userBean");
        LogUtils.d(userBean.getUid()+"");
        Call<CollectBean> myActivityInfo1 = Myapp.shopService.getMyActivityInfo1(userBean.getUid(), 0, 20, 3);
        myActivityInfo1.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                 favorites = response.body().getFavorites();
                LogUtils.d(response.toString());

            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                LogUtils.d(t.toString());
            }
        });
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        LinearLayoutManager manager =new LinearLayoutManager(getActivity());
        collectRv.setLayoutManager(manager);
        collectRv.setAdapter(new RvAdatper());
        return rootView;
    }
    @Override
    protected int getLayout() {
        return R.layout.collect_fra_layou;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    class RvAdatper extends RecyclerView.Adapter<ViewHodler>{

        @Override
        public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(),R.layout.ll_collect,null);
            return new ViewHodler(view);
        }
        @Override
        public void onBindViewHolder(ViewHodler holder, int position) {
            CollectBean.FavoriteBean favoriteBean = favorites.get(3);
            holder.view.setText(favoriteBean.getTitle());
        }

        @Override
        public int getItemCount()
        {
            if(favorites!=null){
                return favorites.size();
            }
            return 0;
        }
    }

    class ViewHodler extends RecyclerView.ViewHolder{
            TextView view;
        public ViewHodler(View itemView) {
            super(itemView);
            view= (TextView) itemView.findViewById(R.id.tv_ll_collcet);
        }
    }
}
